﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace HotelManagement
{
    public partial class RoomsAttributes : Form
    {
        SqlConnection Cnn = new SqlConnection(Globals.DBConnection());
        DataSet ds = new DataSet();
        SqlDataAdapter da;
        int rt_id;

        public RoomsAttributes()
        {
            InitializeComponent();
        }

        private void RoomsAttributes_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select rt_id as [شماره] ,rt_type_name as [نوع اتاق],rt_capacity as [ظرفیت] from Room_Type", Cnn);
            da.Fill(ds);
            DgViewRoomAttr.DataSource = ds.Tables[0];
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(RoomTypeNameTxt.Text) && !string.IsNullOrEmpty(RoomCapacity.Text))
            {
                SqlCommand SaveINDB = new SqlCommand("INSERT INTO Room_Type (rt_type_name,rt_capacity) values (@typename,@capacity)", Cnn);
                SaveINDB.Parameters.AddWithValue("@typename", RoomTypeNameTxt.Text.Trim());
                SaveINDB.Parameters.AddWithValue("@capacity", int.Parse(RoomCapacity.Text.Trim()));

                Cnn.Open();
                SaveINDB.ExecuteNonQuery();
                Cnn.Close();

                ds.Clear();
                da.Fill(ds);
                DgViewRoomAttr.DataSource = ds.Tables[0];
            }
            else 
            {
                MessageBox.Show("مقادیر خالی است");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("آیا برای حذف نوع اتاق مطمئنید؟", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                SqlCommand DeleteCMD = new SqlCommand("DELETE FROM Room_Type WHERE rt_id=@rt_id", Cnn);
                DeleteCMD.Parameters.AddWithValue("@rt_id", rt_id);
                Cnn.Open();
                try
                {
                    DeleteCMD.ExecuteNonQuery();
                }
                catch (SqlException)
                {
                    MessageBox.Show("این نوع در حال استفاده است", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Cnn.Close();
                    return;
                }
                finally
                {
                    Cnn.Close();
                }
                    
                
                Cnn.Close();

                ds.Clear();
                da.Fill(ds);
                DgViewRoomAttr.DataSource = ds.Tables[0];

                MessageBox.Show("حذف نوع اتاق با موفقیت انجام شد");
                button3.Enabled = false;
            }
        }

        private void DgViewRoomAttr_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            rt_id = (int)DgViewRoomAttr.CurrentRow.Cells[0].Value;
            button3.Enabled = true;
        }

    }
}
